<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Inspired from ActionMailer class by Felix Geisendörfer:
 * http://www.fg-webdesign.de
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherPHP
 * @subpackage   Email
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class EmailComponent
{
	protected $sender;
	protected $replyTo;
	protected $recipients = array();
	protected $encoding   = 'quoted-printable';
	protected $headers;
	
	protected $controller;
	
	function startup(Controller $controller)
	{
		$this->headers = array(
			'MIME-Version' => '1.0',
			'X-Mailer'     => 'Feather for PHP 2',
			'Content-Type' => 'text/html; charset='.Config::charset,
		);
		$this->controller = $controller;
	}
	
	// actions
	
	function setHeader($name, $value)
	{
		if (strpos($name, CRLF) === false and strpos($value, CRLF) === false)
			$this->headers[$name] = $value;
	}
	
	function setSender($email, $name=null)
	{
		$this->sender = $this->__rfc2822EmailAddress($email, $name);
	}
	
	function setRecipients($recipients=null, $type='To')
	{
		if (empty($recipients))
			$this->recipients = array();
		
		foreach($recipients as $email => $name)
			$this->addRecipient($email, $name, $type);
	}
	
	function addRecipient($email, $name=null, $type='To')
	{
		$rfc_email = $this->__rfc2822EmailAddress($email, $name);
		if ($rfc_email != false)
			$this->recipients[$type][] = $rfc_email;
	}
	
	function send($subject=null, $headers=null)
	{
		if (empty($subject))
			$subject = _("No subject");
		
		$headers = empty($headers) ?
			$this->headers : array_merge($this->headers, $headers);
		
		// sender
		$headers['From'] = empty($this->sender) ?
			$this->__rfc2822EmailAddress(Config::website_email, Config::website_name) : $this->sender;
		
		if (!empty($this->replyTo))
			$headers['Reply-To'] = $this->replyTo;
		
		// recipients
		$to = implode(', ', $this->recipients['To']);
		
		if (!empty($this->recipients['Cc']))
			$headers['Cc']  = implode(', ', $this->recipients['Cc']);
		
		if (!empty($this->recipients['Bcc']))
			$headers['Bcc'] = implode(', ', $this->recipients['Bcc']);
		
		// message
		$autoRender      = $this->controller->autoRender;
		$alreadyRendered = $this->controller->alreadyRendered;
		
		ob_start();
		$this->controller->render('emails'.DS.$this->controller->action, 'email');
		$body = ob_get_clean();
		
		$this->controller->autoRender      = $autoRender;
		$this->controller->alreadyRendered = $alreadyRendered;
		
		// sends
		$headers = implode_with_keys(': ', "\r\n", $headers);
		$sent    = mb_send_mail($to, $subject, $body, $headers);
		
		//reset
		$this->sender     = null;
		$this->replyTo    = null;
		$this->recipiends = null;
		
		return $sent;
	}
	
	// internals
	
	function __rfc2822EmailAddress($email, $name=null, $encode=true)
	{
		if (strpos($name.$email, CRLF) !== false or
			!preg_match('/\\A(?:^([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i', $email))
		{
			return false;
		}
		$address = is_null($name) ? $email : "$name <$email>";
		
		if ($encode)
			$address = mb_encode_mimeheader($address, Config::charset, $this->encoding);
		
		return $address;
	}
}
?>